package com.huawo.qjs;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class QjsDependence implements Qjs {

    public final static String basePath = "/support_script/qjs/";

    public static List<QjsDependence> baseDependences() {
        List<QjsDependence> list = new ArrayList<>();
        QjsDependence dependence1 = new QjsDependence();
        dependence1.setModule("*");
        dependence1.setName("lv");
        dependence1.setPath("lv");

        QjsDependence dependence2 = new QjsDependence();
        dependence2.setModule("app");
        dependence2.setPath("lvapp");

        list.add(dependence1);
        list.add(dependence2);
        return list;
    }

    public QjsDependence() {}

    public QjsDependence(String module, String path)
    {
        this.module = module;
        this.path = basePath + path;
    }

    public QjsDependence(String module, String name, String path)
    {
        this.module = module;
        this.path = basePath + path;
        this.name = name;
    }

    private String getModuleString()
    {
        String result = "";
        if ("*".equals(getModule()))
        {
            result = "*";
        }
        else
        {
            result = "{ " + getModule() + " }";
        }
        if (getName() != null)
        {
            result += " as " + getName();
        }
        return result;
    }

    private String name;
    private String module;
    private String path;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    @Override
    public String toQjs() {
        StringBuilder sb = new StringBuilder("import ");
        sb.append(getModuleString());
        sb.append(" from ");
        sb.append("\"" + getPath() + "\"");
        return sb.toString();
    }

    @Override
    public boolean equals(@Nullable Object obj) {
        if (obj instanceof QjsDependence) {
            QjsDependence other = (QjsDependence) obj;
            return other.getModule().equalsIgnoreCase(this.getModule());
        }
        return false;
    }
}
